from google.appengine.ext import db
from exceptions import *

class Tax(db.Model):
	
	name = db.StringProperty("")
	value = db.FloatProperty(0)
	vtype = db.StringProperty("")

	def myserialize(self):
		return {
				'key':str(self.key()),
				'name':self.name,
				'value':self.value,
				'type':self.vtype
			}

class InvoiceSettings(db.Model):

	user = db.UserProperty()

	invoiceHeader = db.StringProperty(multiline=True)
	invoiceFooter = db.StringProperty(multiline=True)

	taxes = db.ListProperty(db.Key)#db.ReferenceProperty(reference_class=Tax)

	#rivalsa_inps = db.BooleanProperty(default=False)
	#marcadabollo = db.BooleanProperty(default=False)
	
	@staticmethod
	def getRawInvoiceSettings(user):
		if user:
			invoicesettingsdb = InvoiceSettings.all().filter("user", user).get()
			
			if not invoicesettingsdb:
				invoicesettingsdb = InvoiceSettings(user=user)
				invoicesettingsdb.put()

			return invoicesettingsdb
		else:
			raise NoUserException()

	@staticmethod
	def getTaxes(user, **trash):
		invoicesettingsdb = InvoiceSettings.getRawInvoiceSettings(user)
		return invoicesettingsdb.myserializeTaxes()

	@staticmethod
	def getInvoiceSettings(user, **trash):
		invoicesettingsdb = InvoiceSettings.getRawInvoiceSettings(user)

		if False:
			provatassa = Tax()
			provatassa.name = "prova"
			provatassa.value = 4
			provatassa.vtype = "percentage"
			provatassa.put()

			invoicesettingsdb.taxes.append(provatassa.key())
			invoicesettingsdb.put()

		return invoicesettingsdb.myserialize()

	@staticmethod
	def setInvoiceSettings(user, invoiceHeader=None, invoiceFooter=None, invoiceTaxes=None, **trash):
		if user:
			invoicesettingsdb = InvoiceSettings.all().filter("user", user).get()
			
			if not invoicesettingsdb:
				invoicesettingsdb = InvoiceSettings(user=user)
			
			if invoiceHeader!=None:
				invoicesettingsdb.invoiceHeader = invoiceHeader
				#print "invoiceHeader",invoiceHeader
			if invoiceFooter!=None:
				invoicesettingsdb.invoiceFooter = invoiceFooter
				#print "invoiceFooter",invoiceFooter

			if invoiceTaxes!=None:
				invoicesettingsdb.taxes = []
				for tax in invoiceTaxes:
					taxdb = Tax()
					taxdb.name = tax['name']

					try:
						taxdb.value = float(tax['value'])
					except Exception as ex:
						raise WrongAmountFormatException()

					try:
						taxdb.vtype = tax['type']
						if taxdb.vtype not in ['currency', 'percentage']:
							raise Exception()
					except Exception as ex:
						raise WrongAmountTypeException()

					if taxdb.name:
						taxdb.put()
						invoicesettingsdb.taxes.append(taxdb.key())

			invoicesettingsdb.put()
			
			invoicesettings = invoicesettingsdb.myserialize()
			print invoicesettings
			return invoicesettings
		else:
			raise NoUserException()
	
	def myserialize(self):

		taxes_lst = []

		for key in self.taxes:
			tax = db.get(key)
			taxes_lst.append(tax.myserialize())

		return {
				'invoiceHeader':self.invoiceHeader,
				'invoiceFooter':self.invoiceFooter,
				'invoiceTaxes':taxes_lst
			}

	def myserializeTaxes(self):

		taxes_lst = []

		for key in self.taxes:
			tax = db.get(key)
			taxes_lst.append(tax.myserialize())

		return taxes_lst